package com.erp.erp_project.controller;

import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.model.Customer;
import com.erp.erp_project.model.Order;
import com.erp.erp_project.model.Product;
import com.erp.erp_project.model.Result;
import com.erp.erp_project.service.ICustomService;
import com.erp.erp_project.service.IOrderService;

import com.erp.erp_project.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.classfile.Code;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.lang.reflect.MalformedParameterizedTypeException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author the bright star
 */
@AllArgsConstructor
@Controller
@RequestMapping("/order")
public class OrderController {

    private IOrderService orderService;

    private ICustomService customService;

    private IProductService productService;

    @RequestMapping("/welcome")
    public String welcome(Model model){
        return "welcome";
    }


    @RequestMapping("/picture")
    @Permission("订单修改")
    public String toPicture(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "order/picture";
    }

    @RequestMapping("/file")
    @Permission("订单修改")
    public String toFile(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "order/file";
    }

    @RequestMapping("/tolist")
    public String toList(Model model){
        return "order/orderlist";
    }

    @RequestMapping("/toedit")
    @Permission("订单修改")
    public String toEdit(Model model,@Param("id") Integer id){
        Order order=orderService.getOrderWithOtherById(id);
        model.addAttribute("order",order);
        return "order/edit";
    }
    @RequestMapping("/toadd")
    @Permission("订单添加")
    public String toAdd(Model model){
        return "order/add";
    }

    @RequestMapping("/tocustomer")
    @Permission("订单修改")
    public String toCustomer(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "customer/edit";
    }
    @RequestMapping("/toproduct")
    @Permission("订单修改")
    public String toProduct(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "product/edit";
    }
    @RequestMapping("/torequirement")
    @Permission("订单修改")
    public String toRequirement(Model model,@Param("id") Integer id){
        model.addAttribute("id",id);
        return "order/requirement";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Result<PageInfo<Order>> list(@Param("page") Integer page,@Param("limit") Integer limit){
        PageHelper.startPage(page,limit);
        List<Order> orders=orderService.getOrdersWithOther();
        PageInfo<Order> pageInfo=new PageInfo<>(orders);
        Result<PageInfo<Order>> result=new Result<>(0,"查询成功");
        result.setData(pageInfo);
        return result;
    }

    @ResponseBody
    @RequestMapping("/getpicture")
    @Permission("订单修改")
    public List<String> getPicture(@Param("id") Integer id){
        Order order=orderService.getOrderWithOtherById(id);
        String image=order.getImage();
        if (image!=null){
            String[] imgs=image.split(",");
            List<String> list=new ArrayList<>();
            for (int i=0;i<imgs.length;i++){
                list.add(imgs[i]);
            }
            return list;
        }
        return new ArrayList<>();
    }

    @RequestMapping("/deletepic")
    @ResponseBody
    @Permission("订单修改")
    public Result<String> deletePic(@Param("data") String data,@Param("id") Integer id,@Param("dels") String dels){
        System.out.println(dels);
        String[] delArray=dels.split(",");
        for (String del: delArray) {
            File file=new File("D:/Picture/pic/"+del.substring(5));
            if (file.exists()){
                file.delete();
            }
        }
        orderService.updatePic(id,data);
        return new Result<>(200,"删除成功");
    }

    @RequestMapping("/uploadpic")
    @ResponseBody
    @Permission("订单修改")
    public Result<List<String>> uploadPic(MultipartFile[] files,@Param("id") Integer id){
        System.out.println(files);
        if (files.length==0){
            return new Result<>(100,"没有文件");
        }
        List<String> names=orderService.uploadPic(files,id);
        Result<List<String>> result=new Result<>(200,"上传成功");
        result.setData(names);
        return result;
    }

    @ResponseBody
    @RequestMapping("/getfile")
    @Permission("订单修改")
    public List<String> getFile(@Param("id") Integer id){
        Order order=orderService.getOrderWithOtherById(id);
        String str=order.getFiles();
        String[] files=str.split(",");
        List<String> list=new ArrayList<>();
        for (int i=0;i<files.length;i++){
            list.add(files[i]);
        }
        return list;
    }

    @RequestMapping("/deletefile")
    @ResponseBody
    @Permission("订单修改")
    public Result<String> deleteFile(@Param("data") String data,@Param("id") Integer id){
        System.out.println(data);
        orderService.updateFile(id,data);
        return new Result<>(200,"删除成功");
    }

    @RequestMapping("/uploadfile")
    @ResponseBody
    @Permission("订单修改")
    public Result<List<String>> uploadFile(MultipartFile[] files,@Param("id") Integer id){
        System.out.println(files);
        if (files.length==0){
            return new Result<>(100,"没有文件");
        }
        List<String> names=orderService.uploadFile(files,id);
        Result<List<String>> result=new Result<>(200,"上传成功");
        result.setData(names);
        return result;
    }

    @RequestMapping("/add")
    @ResponseBody
    @Permission("订单添加")
    public Result<String> add(@Param("order") Order order){
        orderService.addOrder(order);
        return new Result<>(200,"添加成功");
    }

    @RequestMapping("/edit")
    @ResponseBody
    @Permission("订单修改")
    public Result<String> edit(Order order){
        orderService.editOrder(order);
        return new Result<>(200,"编辑成功");
    }

    @RequestMapping("/delete")
    @ResponseBody
    @Permission("订单删除")
    public Result<String> delete(String ids){
        String[] integers=ids.split(",");
        List<Integer> list=new ArrayList<>();
        for (String s: integers) {
            list.add(Integer.parseInt(s));
        }
        orderService.deleteOrders(list);
        return new Result<>(20,"删除成功");
    }

    @RequestMapping("/search")
    @ResponseBody
    public Result<PageInfo<Order>> search(Integer page,Integer limit,Integer type,String condition){
        PageHelper.startPage(page,limit);
        List<Order> orders=orderService.searchOrder(type,condition);
        PageInfo<Order> info=new PageInfo<>(orders);
        Result<PageInfo<Order>> result=new Result<>(0,"查询成功");
        result.setData(info);
        return result;
    }

    @RequestMapping("/customernames")
    @ResponseBody
    public Result<List<Customer>> getCustomNames(){
        List<Customer> customers=customService.getAll();
        Result<List<Customer>> result=new Result<>(200,"查询成功");
        result.setData(customers);
        return  result;
    }
    @RequestMapping("/productnames")
    @ResponseBody
    public Result<List<Product>> getProductNames(){
        List<Product> products=productService.getAll();
        Result<List<Product>> result=new Result<>(200,"查询成功");
        result.setData(products);
        return  result;
    }

    @ResponseBody
    @RequestMapping("/loadedit")
    @Permission("订单修改")
    public Order loadEdit(@Param("id") Integer id){
        Order order=orderService.getOrderWithOtherById(id);
        return order;
    }

    @RequestMapping("/requirement")
    @ResponseBody
    public Result<String> requirement(@Param("note") String note,Integer id){
        orderService.setRequirement(note,id);
        return new Result<>(200,"操作成功");
    }

}
